// @Software: serve
// @File : sys_dictionary
// @Author : vince
// @Time : 2021/9/25 7:38
// @Description  请填写文件描述
package v1

import (
	. "gintify/core"
	"gintify/model"
	"gintify/response"
	"github.com/gin-gonic/gin"
	"strconv"
)

type IDictionaryController interface {
	IRestController
}

type DictionaryController struct {
	//空结构体
}

func isDictionaryExist(name string) bool {
	var dict model.SysDictionary
	DB.Debug().Where("name = ?", name).First(&dict)
	return dict.ID != 0
}

// @Summary 新增单词
// @Description 在数据库中新增单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Success 200 {object} []model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts [post]
func (m DictionaryController) Post(c *gin.Context) {
	var dict = model.SysDictionary{}
	if err := c.ShouldBindJSON(&dict); err != nil {
		response.Error(c, "绑定错误", err)
		return
	}
	if err := DB.Create(&dict).Error; err != nil {
		response.Error(c, "创建失败", err)
		return
	}
	response.Success(c, "新建单词成功", dict)

}

// @Summary 获取单词
// @Description 在数据库中获取单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Success 200 {object} []model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts [get]
func (m DictionaryController) GetList(c *gin.Context) {
	var dicts []model.SysDictionary
	if err := DB.Debug().Preload("Words").Find(&dicts).Error; err != nil {
		response.Error(c, "查询失败", err)
		return
	}
	response.Success(c, "获取单词列表成功", dicts)
}

// @Summary 获取单词
// @Description 在数据库中获取单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "dict id in database"
// @Success 200 {object} model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts/{id} [get]
func (m DictionaryController) Get(c *gin.Context) {
	var dict model.SysDictionary
	ids := c.Param("id")
	idx, err := strconv.Atoi(ids)
	if err != nil || idx < 1 {
		response.Fail(c, "URL路径有错误", err)
		return
	}

	if err := DB.Debug().First(&dict, idx).Error; err != nil {
		response.Error(c, "查询失败", err)
		return
	}
	if dict.ID == 0 {
		response.Fail(c, "单词不存在", nil)
		return
	}
	response.Success(c, "获取单词信息成功", dict)

}

// @Summary 更新单词
// @Description 在数据库中更新单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "dict id in database"
// @Success 200 {object} model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts/{id} [put]
func (m DictionaryController) Put(c *gin.Context) {
	ids := c.Param("id")
	idx, err := strconv.Atoi(ids)
	if err != nil || idx < 1 {
		response.Fail(c, "URL路径有错误", err)
		return
	}
	var dict = model.SysDictionary{}
	if err := c.ShouldBindJSON(&dict); err != nil {
		response.Error(c, "绑定错误", nil)
		LG.Errorf("绑定出错%v\n", err)
		return
	}

	if err := DB.Model(&model.SysDictionary{}).Where("id = ?", idx).Updates(&dict).Error; err != nil {
		response.Error(c, "更新失败", err)
		return
	}
	response.Updated(c, "更新单词信息成功", dict)
}

// @Summary 更新单词
// @Description 在数据库中更新单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "dict id in database"
// @Success 200 {object} model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts/{id} [patch]
func (m DictionaryController) Patch(c *gin.Context) {
	panic("implement me")
}

// @Summary 删除单词
// @Description 在数据库中删除单词信息
// @Tags 单词
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "dict id in database"
// @Success 200 {object} model.SysDictionary "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/dicts/{id} [delete]
func (m DictionaryController) Delete(c *gin.Context) {
	ids := c.Param("id")
	idx, err := strconv.Atoi(ids)
	if err != nil || idx < 1 {
		response.Fail(c, "URL路径有错误", err)
		return
	}
	if err:=DB.Debug().Where("dictionary_id = ?", idx).Delete(model.SysWord{}).Error;err!=nil{
		response.Fail(c, "删除该字典下的单词失败", err)
		return
	}
	if err := DB.Debug().Delete(model.SysDictionary{}, idx).Error; err != nil {
		response.Error(c, "删除失败", err)
		return
	}
	response.Deleted(c, "删除成功", nil)
}

func NewDictionaryController() *DictionaryController {
	DB.AutoMigrate(&model.SysDictionary{})
	return &DictionaryController{}
}
